using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVUPRA
    {
        public DataRow Convert2VUPRA(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVUPRA(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VUPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VUPPROJNR"] = " ".PadRight(5 ); 
         dwReturn["VUPADAT"] = " ".PadRight(8 ); 
         dwReturn["VUPPOS"] = " ".PadRight(2 ); 
         dwReturn["VUPKTOANR"] = " ".PadRight(0 ); 
         dwReturn["VUPPRAEMIE"] = " ".PadRight(2 ); 
         dwReturn["VUPABGANG"] = " ".PadRight(0 ); 
         dwReturn["VUPNEUB"] = " ".PadRight(8 ); 
         dwReturn["VUPNEUD"] = " ".PadRight(0 ); 
         dwReturn["VUPAENB"] = " ".PadRight(8 ); 
         dwReturn["VUPAEND"] = " ".PadRight(0 ); 
         dwReturn["VUPAENZL"] = " ".PadRight(0 ); 
         dwReturn["VUPFILLER"] = " ".PadRight(35 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVUPRA(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVUPRA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVUPRA(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVUPRA(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVUPRA(dwLocal, ITXDBVERSION);
            }
            UpdateVUPRA(dwLocal);
        }
        public void InsertVUPRA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VUPRA(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVUPRA(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VUPRA WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VUPRA  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VUPRA";
            Return = Result;
            return Return;
        }
        public void DeleteVUPRA(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VUPRA   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVUPRA(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VUPRA SET "
                    +"VUPMANDANT= :VUPMANDANT,"
                    +"VUPPROJNR= :VUPPROJNR,"
                    +"VUPADAT= :VUPADAT,"
                    +"VUPPOS= :VUPPOS,"
                    +"VUPKTOANR= :VUPKTOANR,"
                    +"VUPPRAEMIE= :VUPPRAEMIE,"
                    +"VUPABGANG= :VUPABGANG,"
                    +"VUPNEUB= :VUPNEUB,"
                    +"VUPNEUD= :VUPNEUD,"
                    +"VUPAENB= :VUPAENB,"
                    +"VUPAEND= :VUPAEND,"
                    +"VUPAENZL= :VUPAENZL,"
                    +"VUPFILLER= :VUPFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VUPMANDANT", OracleType.NVarChar).Value = dwData["VUPMANDANT"].ToString(); 
          or1.Parameters.Add("VUPPROJNR", OracleType.NVarChar).Value = dwData["VUPPROJNR"].ToString(); 
          or1.Parameters.Add("VUPADAT", OracleType.NVarChar).Value = dwData["VUPADAT"].ToString(); 
          or1.Parameters.Add("VUPPOS", OracleType.NVarChar).Value = dwData["VUPPOS"].ToString(); 
          or1.Parameters.Add("VUPKTOANR", OracleType.NVarChar).Value = dwData["VUPKTOANR"].ToString(); 
          or1.Parameters.Add("VUPPRAEMIE", OracleType.NVarChar).Value = dwData["VUPPRAEMIE"].ToString(); 
          or1.Parameters.Add("VUPABGANG", OracleType.NVarChar).Value = dwData["VUPABGANG"].ToString(); 
          or1.Parameters.Add("VUPNEUB", OracleType.NVarChar).Value = dwData["VUPNEUB"].ToString(); 
          or1.Parameters.Add("VUPNEUD", OracleType.NVarChar).Value = dwData["VUPNEUD"].ToString(); 
          or1.Parameters.Add("VUPAENB", OracleType.NVarChar).Value = dwData["VUPAENB"].ToString(); 
          or1.Parameters.Add("VUPAEND", OracleType.NVarChar).Value = dwData["VUPAEND"].ToString(); 
          or1.Parameters.Add("VUPAENZL", OracleType.NVarChar).Value = dwData["VUPAENZL"].ToString(); 
          or1.Parameters.Add("VUPFILLER", OracleType.NVarChar).Value = dwData["VUPFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
